package com.qingcheng.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.qingcheng.dao.ReplyMapper;
import com.qingcheng.pojo.comment.Reply;
import com.qingcheng.service.comment.ReplyService;
import com.qingcheng.util.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;
import java.util.Map;
@Service
public class ReplyServiceImpl implements ReplyService {
    @Autowired
    private ReplyMapper replyMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    /**
     * 根据传输的信息查找所有的评论评价
     * @param commentID

     * @return
     */
    @Override
    public List<Reply> findAllByCommentId(String commentID) {
        //从redis中获取数据
        List<Reply> replyList = (List<Reply>) redisTemplate.boundHashOps("ReplyList").get(commentID);
        if (replyList==null){
            Example example=new Example(Reply.class);
            example.setOrderByClause("create_time DESC");
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("commentId",commentID);
            List<Reply> replies = replyMapper.selectByExample(example);
            //放入redis
            redisTemplate.boundHashOps("ReplyList").put(commentID,replies);
            return replies;
        }else {
            return replyList;
        }

    }
    @Autowired
    private IdWorker idWorker;

    /**
     * 新增评价评论
     * @param reply
     */
    @Override
    public void add(Reply reply) {
        reply.setId(String.valueOf(idWorker.nextId()));
        reply.setCreateTime(new Date());
        replyMapper.insertSelective(reply);
    }

}
